import { mixin_list } from '@tapui/mixins/mixin_list'
import { checkPositiveInt, checkPositiveFloat } from '@/utils/numberValidate'
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export const mixin_list_product = {
  mixins: [mixin_list],
  data() {
    return {
      codeModel: {},
      editModel: false,
      edittempdatas: [],
      baseRules: {
        validateRequire: [
          // 必填
          { required: 'true', message: '请输入内容', trigger: 'change' }
        ],
        checkPositiveInt: [
          // 正整形校验
          { validator: checkPositiveInt }
        ],
        checkPositiveFloat: [
          // 正两位小数校验
          { validator: checkPositiveFloat }
        ],
        checkPositiveIntRequire: [
          { validator: checkPositiveInt },
          { required: 'true', message: '请输入内容', trigger: 'change' }
        ],
        checkPositiveFloatRequire: [
          { validator: checkPositiveFloat },
          { required: 'true', message: '请输入内容', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    whetherForeign: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
      return true
    },
    doQueryCode() {
      if (this.url.queryCodeList) {
        getAction(this.url.queryCodeList, {}).then((res) => {
          if (res.success) {
            this.codeModel = res.result
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      }
    },
    priceOnchange(row, type) {
      if (this.whetherForeign) {
        if (type === 1) {
          row.moneydollar = (row.pricedollar * row.qty).toFixed(2)
        } else if (type === 2) {
          row.moneydollar = (row.qty * row.pricedollar).toFixed(2)
        } else {
          if (row.qty && row.qty !== 0) {
            if (row.qty > 0) {
              let tempPrice = row.moneydollar / row.qty
              if ((tempPrice + "").indexOf(".") !== -1) {
                row.pricedollar = tempPrice.toFixed(2)
              } else {
                row.pricedollar = tempPrice
              }
            }
          }
        }
      } else {
        if (type === 1) {
          row.money = (row.price * row.qty).toFixed(2)
        } else if (type === 2) {
          row.money = (row.qty * row.price).toFixed(2)
        } else {
          if (row.qty && row.qty !== 0) {
            if (row.qty > 0) {
              let tempPrice = row.money / row.qty
              if ((tempPrice + "").indexOf(".") !== -1) {
                row.price = tempPrice.toFixed(2)
              } else {
                row.price = tempPrice
              }
            }
          }
        }
      }
    },
    delOwnId() {
      // 删除前台自造id
      for (var i = 0; i < this.edittempdatas.length; i++) {
        if (this.edittempdatas[i].id) {
          let id = this.edittempdatas[i].id + ''
          if (id.startsWith("del")) {
            delete this.edittempdatas[i].id
          }
        }
      }
    },
    clearProductDataForHistory() {
      for (var i = 0; i < this.edittempdatas.length; i++) {
        this.edittempdatas[i].copysourceid = this.edittempdatas[i].id
        delete this.edittempdatas[i].id
      }
    },
    openEditModel(type) {
      if (type === 1) {
        this.$tap.confirm('退出编辑模式，修改的数据将会丢失，确定要退出吗？')
          .then((_) => {
            this.editModel = false
          }).catch(() => {})
      } else if (type === 2) {
        this.edittempdatas = JSON.parse(JSON.stringify(this.datas))
        this.editModel = true
      }
    },
    deleteData() {
      if (this.selectedRowKeys.length <= 0) {
        this.$tap.alert("请选择数据后删除！")
        return
      }
      if (this.editModel) {
        this.$tap.confirm('确定要删除吗？').then((_) => {
          this.edittempdatas = this.edittempdatas.filter(item => !this.selectedRowKeys.includes(item.id))
        })
          .catch((_) => {
          })
      } else {
        // 不是编辑模式则调用接口删除
        this.doDelete(this.selectedRowKeys.join(","))
      }
    },
    beforeDoDelete() {
      this.datas = this.datas.filter(item => !this.selectedRowKeys.includes(item.id))
      return true
    },
    afterDoDelete() {
      this.$nextTick(() => {
        this.$emit("saveProductInfo")
      })
    }
  }
}
